//
//  HomePage_display_Controller.m
//  Food
//
//  Created by dllo on 15/7/17.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "HomePage_display_Controller.h"
#import "BaseHeader.h"
#import "Step_table_Cell.h"
#import "HomePage_show_model.h"
#import "AFNetworkHandler.h"
#import "Material_detial_Cell.h"
#import "Material_hhhh_Cell.h"
#import "Materials_Model.h"
#import "UIImageView+WebCache.h"
#import "Good_Table_Header_Cell.h"
#import "Good_detial_Table_Cell.h"
#import "General_knowledge_Model.h"
#import "General_knowledge_Table_Cell.h"
#import "MediaPlayer_Table_Cell.h"
#import "HomePage_detailed_model.h"
#import "SQL_DataBase.h"
#import <ShareSDK/ShareSDK.h>
static NSInteger Segindex = 0;
@interface HomePage_display_Controller ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *dataArry;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) UISegmentedControl *seg;

@property (nonatomic, retain) UISegmentedControl *seg1;

@property (nonatomic, retain) NSMutableArray *GoodArry;

@property (nonatomic, retain) NSMutableArray *BadArry;

@property (nonatomic, retain) NSMutableArray *GeneralArry;

@property (nonatomic, retain) UIView *tabbarview;

@property (nonatomic, retain) UIButton *collect;

@property (nonatomic, retain) UIButton *share;


@end

@implementation HomePage_display_Controller

-(void)dealloc{


    [_tableView release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    Segindex = 0;
    self.automaticallyAdjustsScrollViewInsets = NO;
    //self.view.autoresizesSubviews = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.classTitle;
    self.navigationController.navigationBar.translucent = YES;

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"naviBack"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back1)];
    [self Material_Header_Data];
    
    [self creatTableView];
    
    [self CreateTabbarView];
    
    [self stepData];
    
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    MediaPlayer_Table_Cell *cell = (MediaPlayer_Table_Cell *)[self.tableView cellForRowAtIndexPath:index];
    [cell.movieplayer play];
    self.tabBarController.tabBar.hidden = YES;
    //self.view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
}
- (void) CreateTabbarView
{
    self.tabbarview = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 49, ScreenWidth, 49)];
    self.tabbarview.backgroundColor = [UIColor whiteColor];
    //colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]
    [self.view addSubview:self.tabbarview];
    
    self.collect = [[UIButton alloc] initWithFrame:CGRectMake(80 / 375.0 *ScreenWidth, 10, 30, 30)];
    
    [self.collect setBackgroundImage:[UIImage imageNamed:@"disCollect.jpg"] forState:UIControlStateNormal];
    [self.collect setBackgroundImage:[UIImage imageNamed:@"collect.jpg"] forState:UIControlStateSelected];
    
    SQL_DataBase *dbManager = [SQL_DataBase shareInstance];
    [dbManager openDB];
    [dbManager creatTable1];
    HomePage_detailed_model *detailed_model = [dbManager selectCollectID:self.classid];
    if ([detailed_model.classId compare:@""]) {
        self.collect.selected = YES;
    }
    
    [self.collect addTarget:self action:@selector(collectfun:) forControlEvents:UIControlEventTouchUpInside];
    [self.tabbarview addSubview:self.collect];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(110 / 375.0 * ScreenWidth, 30, 20, 20)];
    lable.font = [UIFont systemFontOfSize:10];
    lable.text = @"收藏";
    [self.tabbarview addSubview:lable];
    
    
    self.share = [[UIButton alloc] initWithFrame:CGRectMake(265 / 375.0 * ScreenWidth, 10, 30, 30)];
    self.share.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"share.jpg"]];
    [self.share addTarget:self action:@selector(shares) forControlEvents:UIControlEventTouchUpInside];
    [self.tabbarview addSubview:self.share];
    UILabel *lable1 = [[UILabel alloc] initWithFrame:CGRectMake(295 / 375.0 * ScreenWidth, 30, 20, 20)];
    lable1.font = [UIFont systemFontOfSize:10];
    lable1.text = @"分享";
    [self.tabbarview addSubview:lable1];
}

// 分享
- (void) shares
{
     NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"taiyang" ofType:@"png"];
    
    //构造分享内容
    id<ISSContent> publishContent = [ShareSDK content:self.navigationItem.title
                                       defaultContent:self.navigationItem.title
                                                image:[ShareSDK imageWithPath:imagePath]
                                                title:@"食尚厨房"
                                                  url:@"#"
                                          description:self.navigationItem.title
                                            mediaType:SSPublishContentMediaTypeNews];
    //创建弹出菜单容器
    id<ISSContainer> container = [ShareSDK container];
    //    [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
    
    //弹出分享菜单
    [ShareSDK showShareActionSheet:container
                         shareList:nil
                           content:publishContent
                     statusBarTips:YES
                       authOptions:nil
                      shareOptions:nil
                            result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                
                                if (state == SSResponseStateSuccess)
                                {
                                    NSLog(NSLocalizedString(@"TEXT_ShARE_SUC", @"分享成功"));
                                }
                                else if (state == SSResponseStateFail)
                                {
                                    NSLog(NSLocalizedString(@"TEXT_ShARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
                                }
                            }];

}

- (void)collectfun:(UIButton *)button
{
    //button.selected = !button.selected;
    SQL_DataBase *dbManager = [SQL_DataBase shareInstance];
    [dbManager openDB];
    [dbManager creatTable1];

    if (button.selected == NO) {
        HomePage_detailed_model *model = [[HomePage_detailed_model alloc] init];
        model.classId = self.classid;
        model.name = self.classTitle;
        model.imagePath = self.MVBackGroundimage;
        NSLog(@"##########%@", self.MVBackGroundimage);
        [dbManager insertCollect:model];
        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil  otherButtonTitles:@"确定", nil];
        [alterView show];
        [alterView release];
        button.selected = !button.selected;
    } else
    {
        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您已收藏" delegate:self cancelButtonTitle:nil  otherButtonTitles:@"确定", nil];
        [alterView show];
        [alterView release];
    }
}

- (void) creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64 - 49) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
    [self.view addSubview:self.tableView];
    [self.tableView release];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        MediaPlayer_Table_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"MediaPlayer_Table_Cell"];
        if (!cell) {
            cell = [[MediaPlayer_Table_Cell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"MediaPlayer_Table_Cell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSLog(@"%@", self.model.materialVideoPath);
        [cell setPlayURL:self.model.materialVideoPath image:self.MVBackGroundimage];
        cell.productionProcessPath = self.model.productionProcessPath;
        return cell;
}
    if (indexPath.section == 1) {  
        
        if (self.seg.selectedSegmentIndex == 0) {
            Step_table_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"imaCell"];
            if (!cell) {
                cell = [[Step_table_Cell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"imaCell"];
            }
        cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell setData:self.dataArry[indexPath.row] index:indexPath.row];
            return cell;
        }
        
        if (self.seg.selectedSegmentIndex == 1) {
            Material_hhhh_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Material_hhhh_Cell"];
            if (!cell) {
                cell = [[Material_hhhh_Cell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Material_hhhh_Cell"];
            }
            cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell.rawMaterialimage sd_setImageWithURL:[NSURL URLWithString:self.imagePath]];
            [cell setModel:self.model];
            return cell;
        }
        if (self.seg.selectedSegmentIndex == 2) {
            General_knowledge_Table_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"General_knowledge_Table_Cell"];
            if (!cell) {
                cell = [[General_knowledge_Table_Cell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"General_knowledge_Table_Cell"];
            }
            General_knowledge_Model *model = self.GeneralArry[indexPath.row];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
            [cell setModel:model];
            return cell;
        }
        if (self.seg.selectedSegmentIndex == 3) {
            Good_Table_Header_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Good_Table_Header_Cell"];
            if (!cell) {
                cell = [[Good_Table_Header_Cell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Good_Table_Header_Cell"];
            }
            cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.instrLable.text = self.strgoodHeaderTitle;
            cell.lable.text = @"相宜";
            [cell.image sd_setImageWithURL:[NSURL URLWithString:self.goodHeaderTitle]];
            return cell;
        }
        return nil;
    }
    if (indexPath.section == 2) {
        
        if (self.seg.selectedSegmentIndex == 1) {
            Material_detial_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Material_detial_Cell"];
            if (!cell) {
                cell = [[Material_detial_Cell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Material_detial_Cell"];
            }
            cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            Materials_Model *model = self.MaterialArry[indexPath.row];
            [cell setModel:model];
            return cell;
        }
        if (self.seg.selectedSegmentIndex == 3) {
            Good_detial_Table_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Good_detial_Table_Cell"];
            if (!cell) {
                cell = [[Good_detial_Table_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Good_detial_Table_Cell"];
            }
            cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            Good_Bad_Model *model = self.GoodArry[indexPath.row];
            [cell setModel:model];
            return cell;
        }
        return nil;
    }
    if (indexPath.section == 3) {
        if (self.seg.selectedSegmentIndex == 3) {
            Good_Table_Header_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Good_Table_Header_Cell"];
            if (!cell) {
                cell = [[Good_Table_Header_Cell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Good_Table_Header_Cell"];
            }
            cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.instrLable.text = self.strbadHeaderTitle;;
            cell.lable.text = @"相克";
            NSLog(@"%@", self.strbadHeaderTitle);
            [cell.image sd_setImageWithURL:[NSURL URLWithString:self.badHeaderTitle]];
            return cell;
        }
        return nil;
    }
    if (indexPath.section == 4) {
        if (self.seg.selectedSegmentIndex == 3) {
            Good_detial_Table_Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Good_detial_Table_Cell"];
            if (!cell) {
                cell = [[Good_detial_Table_Cell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Good_detial_Table_Cell"];
            }
            cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            Good_Bad_Model *model = self.BadArry[indexPath.row];
            [cell setModel:model];
            return cell;
        }
        return nil;
    }
    return nil;
}

- (void) stepData
{
    UIActivityIndicatorView *activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
    
    activityIndicator.frame = CGRectMake(0, 0, 100, 100);
    
    activityIndicator.color = [UIColor grayColor];
    
    activityIndicator.center = CGPointMake(ScreenWidth / 2.0, ScreenHeight / 2.0 - 50);
    
    [self.view addSubview:activityIndicator];
    
    [activityIndicator startAnimating];
    
    if (self.dataArry == nil) {
        self.dataArry = [NSMutableArray array];
        NSString *urlStr = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/vegetable/tblvegetable!getIntelligentChoice.do?vegetable_id=%@&type=2&phonetype=0&is_traditional=0", self.classid];
        [AFNetworkHandler getWithAFNByURL:urlStr completion:^(id result) {
            NSArray *tempArry = result[@"data"];
            self.dataArry = [HomePage_show_model arrayWithModelByArray:tempArry];
            [self.tableView reloadData];
            NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
            [activityIndicator stopAnimating];
        }];
    }
    if (self.dataArry.count != 0) {
        [self.tableView reloadData];
        NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
        [activityIndicator stopAnimating];
    }
}


- (void) Material_Header_Data
{
    if (self.model == nil) {
        NSString *str = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/vegetable/tblvegetable!getTblVegetables.do?vegetable_id=%@&phonetype=2&user_id=&is_traditional=0",self.classid];
        [AFNetworkHandler getWithAFNByURL:str completion:^(id result) {
            NSArray *tempArry = result[@"data"];
            NSMutableArray *arry = [HomePage_Material_model arrayWithModelByArray:tempArry];
            self.model = arry[0];
            [self.tableView reloadData];
        }];
    }
    if (self.model != nil) {
        [self.tableView reloadData];
    }
}

- (void) Material_detial_Data
{
    if (self.MaterialArry == nil) {
        self.MaterialArry = [NSMutableArray array];
        NSString *strURL = [NSString stringWithFormat:@"http://121.41.88.179:80/HandheldKitchen/api/vegetable/tblvegetable!getIntelligentChoice.do?vegetable_id=%@&type=1&phonetype=0&is_traditional=0", self.classid];
        NSLog(@"claasid:%@", self.classid);
        [AFNetworkHandler getWithAFNByURL:strURL completion:^(id result) {
            NSArray *tempArry = result[@"data"];
            for (NSDictionary *dic in tempArry) {
                self.imagePath = dic[@"imagePath"];
                NSArray *dataArry = dic[@"TblSeasoning"];
                NSArray *modelArry = [Materials_Model arrayWithModelByArray:dataArry];
                for (Materials_Model *model in modelArry) {
                    [self.MaterialArry addObject:model];
                }
            }
            [self.tableView reloadData];
        }];
    }
    if (self.MaterialArry.count != 0) {
        [self.tableView reloadData];
    }
}

- (void) General_Knowlage_Data
{
    if (self.GeneralArry == nil) {
        self.GeneralArry = [NSMutableArray array];
        NSString *str = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/vegetable/tblvegetable!getIntelligentChoice.do?vegetable_id=%@&type=4&phonetype=0&is_traditional=0", self.classid];
        [AFNetworkHandler getWithAFNByURL:str completion:^(id result) {
            NSArray *tempArry = result[@"data"];
            self.GeneralArry = [General_knowledge_Model arrayWithModelByArray:tempArry];
            [self.tableView reloadData];
        }];
    }
    if (self.GeneralArry.count != 0) {
        [self.tableView reloadData];
    }
}

- (void) GoodBaddata
{
    if (self.GoodArry == nil && self.BadArry == nil) {
        self.GoodArry = [NSMutableArray array];
        self.BadArry = [NSMutableArray array];
        
        NSString *str = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/vegetable/tblvegetable!getIntelligentChoice.do?vegetable_id=%@&type=3&phonetype=0&is_traditional=0", self.classid];
        NSLog(@"id:%@", self.classid);
        [AFNetworkHandler getWithAFNByURL:str completion:^(id result) {
            NSArray *tempArry = result[@"data"];
            if (tempArry.count == 0) {
                
            } else if(tempArry.count == 1)
            {
                NSDictionary *Dic = tempArry[0];
                NSString *number = Dic[@"isFitting"];
                int chang = [number intValue];
                if (chang == 0) {
                    self.goodHeaderTitle = Dic[@"imageName"];
                    self.strgoodHeaderTitle = Dic[@"materialName"];
                    NSArray *goodMaterialArry = Dic[@"Fitting"];
                    self.GoodArry = [Good_Bad_Model arrayWithModelByArray:goodMaterialArry];
                }
                if (chang == 1) {
                    self.badHeaderTitle = Dic[@"imageName"];
                    NSArray *badMaterialArry = Dic[@"Gram"];
                    self.strbadHeaderTitle = Dic[@"materialName"];
                    self.BadArry = [Good_Bad_Model arrayWithModelByArray:badMaterialArry];
                }
            } else if (tempArry.count == 2)
            {
                NSDictionary *Dic = tempArry[0];
                NSString *number = Dic[@"isFitting"];
                int chang = [number intValue];
                if (chang == 0) {
                    self.goodHeaderTitle = Dic[@"imageName"];
                    self.strgoodHeaderTitle = Dic[@"materialName"];
                    NSArray *goodMaterialArry = Dic[@"Fitting"];
                    self.GoodArry = [Good_Bad_Model arrayWithModelByArray:goodMaterialArry];
                }
                if (chang == 1) {
                    self.badHeaderTitle = Dic[@"imageName"];
                    NSArray *badMaterialArry = Dic[@"Gram"];
                    self.strbadHeaderTitle = Dic[@"materialName"];
                    self.BadArry = [Good_Bad_Model arrayWithModelByArray:badMaterialArry];
                }
                
                NSDictionary *Dic1 = tempArry[1];
                NSString *number1 = Dic1[@"isFitting"];
                int chang1 = [number1 intValue];
                if (chang1 == 0) {
                    self.goodHeaderTitle = Dic1[@"imageName"];
                    self.strgoodHeaderTitle = Dic1[@"materialName"];
                    NSArray *goodMaterialArry = Dic1[@"Fitting"];
                    self.GoodArry = [Good_Bad_Model arrayWithModelByArray:goodMaterialArry];
                }
                if (chang1 == 1) {
                    self.badHeaderTitle = Dic1[@"imageName"];
                    NSArray *badMaterialArry = Dic1[@"Gram"];
                    self.strbadHeaderTitle = Dic1[@"materialName"];
                    self.BadArry = [Good_Bad_Model arrayWithModelByArray:badMaterialArry];
                }
            }
            
            if (self.GoodArry.count != 0) {
                self.strgoodHeaderTitle = [NSString stringWithFormat:@"  %@与下列食物相宜",self.strgoodHeaderTitle];
            }
            if (self.GoodArry.count == 0) {
                self.strbadHeaderTitle = [NSString stringWithFormat:@"  无相宜内容"];
            }
            
            if (self.BadArry.count != 0) {
                self.strbadHeaderTitle = [NSString stringWithFormat:@"  %@与下列食物相克",self.strbadHeaderTitle];
            }
            if (self.BadArry.count == 0) {
                self.strbadHeaderTitle = [NSString stringWithFormat:@"  无相克内容"];
            }
            [self.tableView reloadData];
        }];
    }
    if (self.GoodArry.count != 0 || self.BadArry.count != 0) {
        [self.tableView reloadData];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    if (section == 1) {
        if (self.seg.selectedSegmentIndex == 0) {
             return self.dataArry.count;
        }
        if (self.seg.selectedSegmentIndex == 1) {
            return 1;
        }
        if (self.seg.selectedSegmentIndex == 2) {
            return 1;
        }
        if (self.seg.selectedSegmentIndex == 3) {
            return 1;
        }
    }
    if (section == 2) {
        if (self.seg.selectedSegmentIndex == 1) {
           return self.MaterialArry.count;
        }
        if (self.seg.selectedSegmentIndex == 3) {
            return self.GoodArry.count;
        }
    }
    if (section == 3) {
        if (self.seg.selectedSegmentIndex == 3) {
           return 1;
        }
    }
    if (section == 4) {
        if (self.seg.selectedSegmentIndex == 3) {
            return self.BadArry.count;
        }
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 50;
    }
    return 0;
}


#warning mark - cell 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 200 / 667.0 * ScreenHeight;
    }
    if (indexPath.section == 1) {
        if (self.seg.selectedSegmentIndex == 0) {
            return 200;
        }
        if (self.seg.selectedSegmentIndex == 1) {
            return 340 ;
        }
        if (self.seg.selectedSegmentIndex == 2) {
            return ScreenHeight + 100;
        }
        if (self.seg.selectedSegmentIndex == 3) {
            return 70 / 375.0 * ScreenWidth;
        }
    }
    if (indexPath.section == 2) {
        if (self.seg.selectedSegmentIndex == 1) {
            return 160;
        }
        if (self.seg.selectedSegmentIndex == 3) {
            return 70 / 375.0 * ScreenWidth ;
        }
    }
    if (indexPath.section == 3) {
        if (self.seg.selectedSegmentIndex == 3) {
            return 70 / 375.0 * ScreenWidth ;
        }
    }
    if (indexPath.section == 4) {
        if (self.seg.selectedSegmentIndex == 3) {
            return 70 / 375.0 * ScreenWidth ;
        }
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
        self.seg = [[UISegmentedControl alloc] initWithItems:@[@"制作步骤", @"所需材料", @"相关常识", @"相克相宜"]];
        self.seg.frame = CGRectMake(0, 0, ScreenWidth, 50 / 375.0 * ScreenWidth);
        [self.view addSubview:self.seg];
        self.seg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backGround.jpg"]];
//        self.seg.tintColor = [UIColor grayColor];
        self.seg.tintColor = [UIColor orangeColor];
        self.seg.selectedSegmentIndex = Segindex;
        [self.seg addTarget:self action:@selector(fun) forControlEvents:UIControlEventValueChanged];
        [view addSubview:self.seg];
        return view;
    }
    return nil;
}


- (void)fun
{
    Segindex = self.seg.selectedSegmentIndex;
    if (self.seg.selectedSegmentIndex == 0) {
        [self stepData];
    }
    if (self.seg.selectedSegmentIndex == 1) {
        [self Material_Header_Data];
        [self Material_detial_Data];
    }
    if (self.seg.selectedSegmentIndex == 2) {
        [self General_Knowlage_Data];
    }
    if (self.seg.selectedSegmentIndex == 3) {
        [self GoodBaddata];
    }
}

- (void) back1
{
    [self.navigationController popToRootViewControllerAnimated:YES];
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    MediaPlayer_Table_Cell *cell = (MediaPlayer_Table_Cell *)[self.tableView cellForRowAtIndexPath:index];
    [cell.movieplayer stop];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
